﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Skyworth.CRM.Common;
using Skyworth.CRM.Service.IBLL;

namespace Skyworth.CRM.Web.SalesManagement
{
    public partial class EditAdjustCredit : BasePage
    {

        ICustomerBLL custBLL = (ICustomerBLL)IocContainer.Instance.Resolve<ICustomerBLL>();
        IWeekLongBLL weekLongBLL = IocContainer.Instance.Resolve<IWeekLongBLL>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //if ("update" == Request.QueryString["Type"].ToString().ToLower())
                //{

                //    int empId = 0;
                //    int.TryParse(Request.QueryString["EmpId"], out empId);
                //    if (empId > 0)
                //    {
                //        DataTable dt = weekLongBLL.QueryCredit(" and  a.Fitemid=" + empId);
                //        if (dt != null && dt.Rows.Count > 0)
                //        {
                //            this.txtCustomerName.Text = dt.Rows[0]["FName"].ToString();
                //            this.txtStartDate.Text = dt.Rows[0]["FBeginDate"].ToString();
                //            this.txtEndDate.Text = dt.Rows[0]["FEndDate"].ToString();
                //            this.ddlFCreditType.SelectedValue = dt.Rows[0]["FCreditType"].ToString();
                //            this.ddlFtype.SelectedValue = dt.Rows[0]["Ftype"].ToString();
                //            this.ddlFisYes.SelectedValue = dt.Rows[0]["FisYes"].ToString();
                //            this.txtFamount.Text = dt.Rows[0]["Famount"].ToString();
                //        }
                //        this.txtCustomerName.Attributes.Add("readonly", "readonly");
                //    }


                //    int customerid=0;
                //    int.TryParse(Request.QueryString["cid"], out customerid);
                //    DataTable dtCash = weekLongBLL.QueryCash(" and Fcustid=" + customerid);
                //    if (dtCash != null && dtCash.Rows.Count > 0)
                //    {
                //        this.lbFCashAmount.Text = dtCash.Rows[0]["FCashAmount"].ToString();
                //        this.lbFcreditAmount.Text = dtCash.Rows[0]["FcreditAmount"].ToString();
                //    }

                //}
                //else
                //{
                //    this.tbMoney.Visible = false;
                //}
            }

        }



        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                Boolean isOK = false;
                //if ("add" == Request.QueryString["Type"].ToString().ToLower())
                //{
                Int32 custid = 0;
                DataTable dtcust = custBLL.Query(" and FName='" + this.txtCustomerName.Text.Trim() + "'");
                if (dtcust != null && dtcust.Rows.Count > 0)
                {
                    custid = Convert.ToInt32(dtcust.Rows[0]["Fitemid"].ToString());
                }
                weekLongBLL.InsertCredit(Convert.ToInt32(this.ddlFCreditType.SelectedValue.ToString()), custid, Convert.ToInt32(this.ddlFtype.SelectedValue.ToString()), Convert.ToDateTime(this.txtStartDate.Text), Convert.ToDateTime(this.txtEndDate.Text), Convert.ToDecimal(this.txtFamount.Text), Convert.ToInt32(this.ddlFCreditWay.SelectedValue), Convert.ToDecimal(this.txtFYearAmount.Text.Trim() == "" ? "0" : this.txtFYearAmount.Text.Trim()), Convert.ToInt32(this.ddlFisYes.SelectedValue.ToString()));
                isOK = true;
                //}
                //else if ("update" == Request.QueryString["Type"].ToString().ToLower())
                //{
                //    int empId = 0;
                //    int customerid=0;
                //    int.TryParse(Request.QueryString["EmpId"], out empId);
                //    int.TryParse(Request.QueryString["cid"], out customerid);
                //    weekLongBLL.UpdateCredit(empId, Convert.ToInt32(this.ddlFCreditType.SelectedValue.ToString()), customerid, Convert.ToInt32(this.ddlFtype.SelectedValue.ToString()), Convert.ToDateTime(this.txtStartDate.Text), Convert.ToDateTime(this.txtEndDate.Text), Convert.ToDecimal(this.txtFamount.Text), 1, 0, Convert.ToInt32(this.ddlFisYes.SelectedValue.ToString()));
                //    isOK = true;
                //}

                if (isOK)
                {
                    ClientScript.RegisterClientScriptBlock(this.GetType(), "closeThisPage", "<script language=\"JavaScript\">parent.addWindow.close();parent.location.href = parent.location.href;</script>");
                }
            }
            catch (Exception exception)
            {
                Logger.Instance.Error(exception);
                Alert("发生异常：\r\n" + exception.Message);
            }

        }
    }
}
